load(":crosstool.bzl", "emscripten_cc_toolchain_config_rule")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "common-script-includes",
    srcs = [
        "emar.sh",
        "emcc.sh",
        "emscripten_config",
        "env.sh",
        "@emscripten//:all",
        "@nodejs//:node_files",
        "@npm//:node_modules",
    ],
)

filegroup(
    name = "compile-emscripten",
    srcs = [":common-script-includes"],
)

filegroup(
    name = "link-emscripten",
    srcs = [
        "emcc_link.sh",
        "link_wrapper.py",
        ":common-script-includes",
        "@emscripten//:all",
        "@nodejs//:node_files",
    ],
)

filegroup(
    name = "every-file",
    srcs = [
        ":compile-emscripten",
        ":link-emscripten",
        "@emscripten//:all",
        "@nodejs//:node_files",
    ],
)

filegroup(name = "empty")

# dlmalloc.bc is implictly added by the emscripten toolchain
cc_library(name = "malloc")

emscripten_cc_toolchain_config_rule(
    name = "wasm",
    cpu = "wasm",
    emscripten_version = "emscripten",
)

cc_toolchain(
    name = "cc-compiler-wasm",
    all_files = ":every-file",
    ar_files = ":common-script-includes",
    as_files = ":empty",
    compiler_files = ":compile-emscripten",
    dwp_files = ":empty",
    linker_files = ":link-emscripten",
    objcopy_files = ":empty",
    strip_files = ":empty",
    toolchain_config = "wasm",
    toolchain_identifier = "emscripten-wasm",
)

cc_toolchain_suite(
    name = "everything",
    toolchains = {
        "wasm": ":cc-compiler-wasm",
        "wasm|emscripten": ":cc-compiler-wasm",
    },
)

py_binary(
    name = "wasm_binary",
    srcs = ["wasm_binary.py"],
)
